using System.Collections.Generic;
using UnityEngine;

public class PlatformMobile : MonoBehaviour
{
	private enum ActionType
	{
		none = 0,
		delay = 1,
		move = 2,
		pause = 3
	}

	public Vector3 startPositionOffset;

	public Vector3 finishPositionOffset;

	private Vector3 startPosition;

	private Vector3 finishPosition;

	public float delay;

	public float speed = 1f;

	public float pause;

	public List<GameObject> dependencies;

	public List<Vector3> dependenciesOffsets;

	private ActionType actionCurrent;

	private float actionCurrentStartTime;

	private float actionCurrentDuration;

	private float moveDuration;

	private Vector3 initialPosition;

	private Vector3 currentPosition;

	private Vector3 targetPosition;

	private bool init = true;

	private bool isActive;

	private void Awake()
	{
		LevelController.LevelReadyEvent += OnLevelReady;
		EventManager.WorldResetEvent += OnWorldReset;
	}

	private void OnDestroy()
	{
		LevelController.LevelReadyEvent -= OnLevelReady;
		EventManager.WorldResetEvent -= OnWorldReset;
	}

	private void OnLevelReady(Transform levelTransform)
	{
		if (base.transform.root == levelTransform)
		{
			initialPosition = base.transform.position;
			startPosition = base.transform.position + startPositionOffset;
			finishPosition = base.transform.position + finishPositionOffset;
			isActive = true;
		}
	}

	private void OnWorldReset()
	{
		base.transform.position = initialPosition;
		init = true;
	}

	private void FixedUpdate()
	{
		if (Time.fixedDeltaTime < 0.001f || !isActive)
		{
			return;
		}
		if (init)
		{
			currentPosition = base.transform.position;
			targetPosition = startPosition;
			moveDuration = Vector3.Distance(startPosition, finishPosition) / speed;
			if (delay > 0f)
			{
				actionCurrent = ActionType.delay;
				actionCurrentDuration = delay;
			}
			else
			{
				actionCurrent = ActionType.move;
				if (currentPosition == startPosition)
				{
					targetPosition = finishPosition;
					actionCurrentDuration = moveDuration;
				}
				else
				{
					targetPosition = startPosition;
					actionCurrentDuration = Vector3.Distance(currentPosition, startPosition) / speed;
				}
			}
			actionCurrentStartTime = Time.time;
			init = false;
		}
		if (init)
		{
			return;
		}
		if (actionCurrent == ActionType.move)
		{
			float maxDistanceDelta = Time.fixedDeltaTime * speed;
			currentPosition = Vector3.MoveTowards(currentPosition, targetPosition, maxDistanceDelta);
			base.GetComponent<Rigidbody>().MovePosition(currentPosition);
			if (dependencies != null)
			{
				for (int i = 0; i < dependencies.Count; i++)
				{
					dependencies[i].GetComponent<Rigidbody>().MovePosition(currentPosition + dependenciesOffsets[i]);
				}
			}
		}
		if (!(Time.time - actionCurrentStartTime - actionCurrentDuration >= (0f - Time.fixedDeltaTime) / 2f))
		{
			return;
		}
		if (actionCurrent == ActionType.delay)
		{
			actionCurrent = ActionType.move;
			if (currentPosition == startPosition)
			{
				targetPosition = finishPosition;
				actionCurrentDuration = moveDuration;
			}
			else
			{
				targetPosition = startPosition;
				actionCurrentDuration = Vector3.Distance(currentPosition, startPosition) / speed;
			}
		}
		else if (actionCurrent == ActionType.move)
		{
			currentPosition = targetPosition;
			base.GetComponent<Rigidbody>().MovePosition(currentPosition);
			if (pause > 0f)
			{
				actionCurrent = ActionType.pause;
				actionCurrentDuration = pause;
			}
			else
			{
				actionCurrent = ActionType.move;
				actionCurrentDuration = moveDuration;
				if (targetPosition == startPosition)
				{
					targetPosition = finishPosition;
				}
				else
				{
					targetPosition = startPosition;
				}
			}
		}
		else if (actionCurrent == ActionType.pause)
		{
			actionCurrent = ActionType.move;
			actionCurrentDuration = moveDuration;
			if (targetPosition == startPosition)
			{
				targetPosition = finishPosition;
			}
			else
			{
				targetPosition = startPosition;
			}
		}
		actionCurrentStartTime = Time.time;
	}
}
